/******************************************************************************
 * @File name   :      PackageInventoryDaoImpl.java
 *
 * @Author      :      JIZHANGS
 *
 * @Date        :      Jul 23, 2015
 *
 * @Copyright Notice: 
 * Copyright (c) 2015 Capgemini, Inc. All  Rights Reserved.
 * This software is published under the terms of the Capgemini Software
 * License version 1.0, a copy of which has been included with this
 * distribution in the LICENSE.txt file.
 * 
 * 
 * ----------------------------------------------------------------------------
 * Date                   Who         Version        Comments
 * Jul 23, 2015 4:18:28 PM        JIZHANGS     1.0            Initial Version
 *****************************************************************************/
package com.cap.bts.crmdp.dealer.purchase.dao.impl;

import java.util.List;

import javax.inject.Inject;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Repository;

import com.cap.bts.crmdp.dealer.purchase.dao.IPackageInventoryDao;
import com.cap.bts.crmdp.dealer.purchase.domain.PackageInventory;
import com.cap.bts.framework.common.dao.impl.BaseDaoImpl;
import com.cap.bts.framework.common.exception.AppException;

/**
 *
 */
@Repository("packageInventoryDao")
public class PackageInventoryDaoImpl extends BaseDaoImpl<PackageInventory, Long> implements IPackageInventoryDao {
    /**
     * @Date        :      Jul 23, 2015
     * @param clazz
     */
    @Inject
    public PackageInventoryDaoImpl(@Value("com.cap.bts.crmdp.dealer.purchase.domain.PackageInventory")  Class<PackageInventory> clazz) {
        super(clazz);
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Jul 23, 2015
     * @see com.cap.bts.crmdp.dealer.purchase.dao.IPackageInventoryDao#getPackageInventoryInfo()
    **/
    @Override
    public PackageInventory getPackageInventoryInfo(PackageInventory packageInventory) throws AppException {
        PackageInventory pi=packageInventory;
        List<PackageInventory> list=super.getListByQuery("from PackageInventory pi where pi.packageDefId="+packageInventory.getPackageDefId()+"and pi.dealerId="+packageInventory.getDealerId());
        if(list!=null && !list.isEmpty()){
            pi=list.get(0);
        }else{
            pi=null;
        }
        
        return  pi;
    }

    /**
     * {@inheritDoc} 
     * overridden:
     * @Date        :      Jul 24, 2015
     * @see com.cap.bts.crmdp.dealer.purchase.dao.IPackageInventoryDao#updatePackageInventoryInfo(com.cap.bts.crmdp.dealer.purchase.domain.PackageInventory)
    **/
    @Override
    public void updatePackageInventoryInfo(PackageInventory packageInventory) throws AppException {
        PackageInventory pi=packageInventory;
        List<PackageInventory> list= super.getListByQuery("from PackageInventory a where a.dealerId= "+packageInventory.getDealerId()+"and a.packageDefId="+packageInventory.getPackageDefId());
        if(list!=null && !list.isEmpty()){
            pi=list.get(0);
            pi.setOnhandQuantity(packageInventory.getOnhandQuantity());
            super.update(pi);
        }
    }
}
